package java_thinking.unit_16;

import java_thinking.net.mindview.util.CountingGenerator;
import java_thinking.unit_15._15_3.Generator;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @Desc:   对每个基本类型都创建一个数组，然后用CountingGenerator来填充每个数组并打印所有的数组
 * @author: cww
 * @DateTime: 2019-04-09 20:36
 */

class Fill {
    public static void primitive(Object array, Generator<?> gen) {
        int size = Array.getLength(array);
        //可以取得一个数组的Class对象
        Class<?> type = array.getClass().getComponentType();
        for (int i=0;i<size;i++) {
            if (type == Boolean.class) {
                Array.setBoolean(array,i,(Boolean) gen.next());
            }else if (type == Byte.class) {
                Array.setByte(array, i, (Byte) gen.next());
            }else if (type == Short.TYPE)
                    Array.setShort(array, i, (Short)gen.next());
                else if (type == Integer.TYPE)
                    Array.setInt(array, i, (Integer)gen.next());
                else if (type == Character.TYPE)
                    Array.setChar(array, i, (Character)gen.next());
                else if (type == Float.TYPE)
                    Array.setFloat(array, i, (Float)gen.next());
                else if (type == Double.TYPE)
                    Array.setDouble(array, i, (Double)gen.next());
                else if (type == Long.TYPE)
                    Array.setLong(array, i, (Long)gen.next());
        }
    }
}
public class E14_PrimitiveArraysFill {
    public static void main(String[] args) {
        int size = 6;
        // First create all primitive arrays
        boolean[] a1 = new boolean[size];
        byte[] a2 = new byte[size];
        char[] a3 = new char[size];
        short[] a4 = new short[size];
        int[] a5 = new int[size];
        long[] a6 = new long[size];
        float[] a7 = new float[size];
        double[] a8 = new double[size];
        System.out.println(("a1 = " + Arrays.toString(a1)));

        Fill.primitive(a1,new CountingGenerator.Boolean());
        System.out.println(("a1 = " + Arrays.toString(a1)));
    }


}
